package org.smile.db.spring;

import org.smile.db.DataSourceProvider;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import javax.sql.DataSource;

/**
 * 使用spring配置名称提供连接池
 */
public class SpringDataSourceProvider implements DataSourceProvider, BeanFactoryAware {

    String dataSourceName;

    BeanFactory beanFactory;

    @Override
    public DataSource get() {
        return beanFactory.getBean(dataSourceName,DataSource.class);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory=beanFactory;
    }

    /**
     * 连接池配置的bean名称
     * @param dataSourceName
     */
    public void setDataSourceName(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }
}
